package com.lys.emsystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lys.emsystem.entity.Admin;
import com.lys.emsystem.entity.Student;
import com.lys.emsystem.entity.Teacher;
import com.lys.emsystem.service.*;
import com.lys.emsystem.utils.Addr;
import com.lys.emsystem.utils.JWTUtils;
import com.lys.emsystem.utils.Result;
import com.lys.emsystem.vo.LoginFormParam;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/emSystem")
@Api(value = "登录")
public class LoginController {
    @Autowired
    private IAdminService iAdminService;

    @Autowired
    private IRoleService iRoleService;

    @Autowired
    private ITeacherService ITeacherService;

    @Autowired
    private IStudentService iStudentService;

    @Autowired
    private ILoginLogService loginLogService;


    @PostMapping("/login")
    public Result login(@RequestBody LoginFormParam loginFormParam, HttpServletRequest request) {
        System.out.println("登录ip:"+ Addr.getIpAddr(request));
    //    获取登录角色
        Integer roleId = loginFormParam.getRoleId();
        //    根据角色去相应的表校验账号密码
    //    获取密码
        HashMap<String, Object> map = new HashMap<>();
        String password = DigestUtils.md5DigestAsHex(loginFormParam.getPassword().getBytes());
        switch (roleId) {
            case 1:
                QueryWrapper<Student> wrapper2 = new QueryWrapper<>();
                wrapper2.eq("student_account", loginFormParam.getAccount());
                wrapper2.eq("password", password);
                Student student = iStudentService.getOne(wrapper2);
                if (student == null) {
                    return Result.buildResult(Result.Status.ERROR, "账号或者密码错误!");
                }else {
                    loginLogService.registerLoginLog(student.getName(), roleId, request);
                    String token = JWTUtils.createToken(student.getId(), roleId);
                    map.put("token", token);
                    map.put("roleId", roleId);
                    return Result.buildResult(Result.Status.OK, map);
                }
            case 2 :
                QueryWrapper<Teacher> wrapper1 = new QueryWrapper<>();
                wrapper1.eq("account", loginFormParam.getAccount());
                wrapper1.eq("password", password);
                Teacher teacher = ITeacherService.getOne(wrapper1);
                if (teacher == null) {
                    return Result.buildResult(Result.Status.ERROR, "账号或者密码错误!");
                }else {
                    loginLogService.registerLoginLog(teacher.getName(), roleId, request);
                    String token = JWTUtils.createToken(teacher.getId(), roleId);
                    map.put("token", token);
                    map.put("roleId", roleId);
                    return Result.buildResult(Result.Status.OK, map);
                }
            case 3 :
                //查询用户
                QueryWrapper<Admin> wrapper = new QueryWrapper<>();
                wrapper.eq("account", loginFormParam.getAccount());
                wrapper.eq("password", password);
                Admin admin = iAdminService.getOne(wrapper);
                if (admin == null) {
                    return Result.buildResult(Result.Status.ERROR, "账号或者密码错误!");
                }else {
                    loginLogService.registerLoginLog(admin.getName(), roleId, request);
                    String token = JWTUtils.createToken(admin.getId(), roleId);
                    map.put("token", token);
                    map.put("roleId", roleId);
                    return Result.buildResult(Result.Status.OK, map);
                }
        }
        return null;
    }

    @GetMapping("/info")
    public Result getInfo(@RequestHeader("token") String token) {
        Map<String, Object> map = JWTUtils.checkToken(token);
        Integer roleId = (Integer) map.get("roleId");

        switch (roleId) {
            case 1:
                QueryWrapper<Student> wrapper2 = new QueryWrapper<>();
                wrapper2.eq("id", map.get("userId"));
                Student student = iStudentService.getOne(wrapper2);
                if (student == null) {
                    return Result.buildResult(Result.Status.NOT_FOUND);
                }else {
                    return Result.buildResult(Result.Status.OK, student);
                }
            case 2 :
                QueryWrapper<Teacher> wrapper1 = new QueryWrapper<>();
                wrapper1.eq("id", map.get("userId"));
                Teacher teacher = ITeacherService.getOne(wrapper1);
                if (teacher == null) {
                    return Result.buildResult(Result.Status.NOT_FOUND);
                }else {
                    return Result.buildResult(Result.Status.OK, teacher);
                }
            case 3 :
                //查询用户
                QueryWrapper<Admin> wrapper = new QueryWrapper<>();
                wrapper.eq("id", map.get("userId"));
                Admin admin = iAdminService.getOne(wrapper);
                if (admin == null) {
                    return Result.buildResult(Result.Status.NOT_FOUND);
                }else {
                    return Result.buildResult(Result.Status.OK, admin);
                }
        }
        return null;
    }
}
